package com.zpy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpy.pojo.Role;
import com.zpy.pojo.Unit;
import com.zpy.pojo.User;
import com.zpy.pojo.UserRole;
import com.zpy.service.RoleService;
import com.zpy.service.UnitService;
import com.zpy.service.UserRoleService;
import com.zpy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("unit")
public class UnitController {

    @Autowired
    private UnitService unitService;

    @RequestMapping("listUnit")
    public String listBook(@RequestParam(required = false,value ="pageNum",defaultValue = "1")Integer pageNum,
                                   @RequestParam(required = false,value ="pageSize",defaultValue = "10")Integer pageSize, Model model, Unit unit){

        if (pageNum<0 || pageNum.equals("") || pageNum==null){
            pageNum=1;
        }
        if (pageSize<0 || pageSize.equals("") || pageSize==null){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Unit>qw=new QueryWrapper<>();
        if (unit.getUnitName()!=null){
            qw.like("unit_name",unit.getUnitName());
        }
        List<Unit> list = unitService.list(qw);
        PageInfo<Unit> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "unit-list";

    }

    //跳转到添加单位的页面
    @RequestMapping("preSaveUnit")
    public String preSaveReader(){
        return "unit-save";
    }
    //添加单位
    @RequestMapping("saveUnit")
    public String save(Unit unit){
        boolean save = unitService.save(unit);
        return "redirect:/unit/listUnit";
    }



    //跳转到修改单位的页面
    @RequestMapping("preUpdateUnit/{id}")
    public String preUpdateReader(@PathVariable Integer id,Model model){

        Unit byId = unitService.getById(id);
        model.addAttribute("unit",byId);
        return "unit-update";
    }

    //修改单位
    @RequestMapping("updateUnit")
    public String updateReader(Unit unit){
        boolean b = unitService.updateById(unit);
        return "redirect:/unit/listUnit";
    }

    //删除单位
    @RequestMapping("delUnit/{id}")
    public String delReader(@PathVariable Integer id){
        boolean b = unitService.removeById(id);
        return "redirect:/unit/listUnit";
    }

    //详情
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id,Model model){
        Unit byId = unitService.getById(id);
        model.addAttribute("unit",byId);
        return "unit-detail";
    }
}
